﻿#pragma once

//#include <msclr\lock.h>
#include <mutex>
#include "Direct3DBase.h"
#include "Color.h"
#include "Vector2.h"
#include "Vector4.h"
#include "Texture.h"

#include "..\..\DirectXTK\Inc\Audio.h"
#include "..\..\DirectXTK\Inc\CommonStates.h"
#include "..\..\DirectXTK\Inc\Effects.h"
#include "..\..\DirectXTK\Inc\GeometricPrimitive.h"
#include "..\..\DirectXTK\Inc\Model.h"
#include "..\..\DirectXTK\Inc\PrimitiveBatch.h"
#include "..\..\DirectXTK\Inc\SpriteBatch.h"
#include "..\..\DirectXTK\Inc\SpriteFont.h"
#include "..\..\DirectXTK\Inc\VertexTypes.h"

#include <map>

using namespace PhoneXamlDirect3DApp1Comp;

// This class renders a simple spinning cube.
ref class CubeRenderer sealed : public Direct3DBase
{
	Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pTextureView;

	ID3D11ShaderResourceView* CreateSquareTexture(int width, int height, int* data);

	// New stuff.
	std::unique_ptr<DirectX::CommonStates>                                  m_states;
	std::unique_ptr<DirectX::SpriteBatch>                                   m_sprites;
	std::unique_ptr<DirectX::PrimitiveBatch<DirectX::VertexPositionColorTexture>>  m_batch;
	std::unique_ptr<DirectX::BasicEffect>                                   m_batchEffect;
	Microsoft::WRL::ComPtr<ID3D11InputLayout>                               m_batchInputLayout;

	std::map<int, ID3D11ShaderResourceView*>								m_textures;
	Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>						m_nullTexture;

	DirectX::XMFLOAT4X4 m_world;
	DirectX::XMFLOAT4X4 m_view;
	DirectX::XMFLOAT4X4 m_projection;

	ID3D11BlendState* NonPremultiplied;
public:
	CubeRenderer();

	void _StopRender();
	void _StartRender();

	// Direct3DBase methods.
	virtual void CreateDeviceResources() override;
	virtual void CreateWindowSizeDependentResources() override;
	virtual void Render() override;

	// Method for updating time-dependent objects.
	void Update(float timeTotal, float timeDelta);

	void RegisterTexture(Texture^ texture);
	void UnregisterTexture(Texture^ texture);
	void UpdateTexture(Texture^ texture);
	void BeginRender();
	void Draw(Vector4 rectangle, Color color);
	void Draw(Vector4 rectangle, Color color, Texture^ texture, Vector4 destinationTextureUV);
	void DrawLine(Vector2 start, Vector2 end, Color color);
	void DrawCircle(Vector2 position, double radius, Color color);
	void DrawTriangles(Vector2* vertices, int verticesLength, unsigned short* indices, int indicesLength, ColorNative* colors, int colorLength);
	void EndRender();

private:
	bool m_loadingComplete;
	//msclr::lock lock;
	std::mutex lock;

	//struct Batch
	//{
	//	Vector4 rectangle;
	//	Color color;
	//	int textureId;
	//
	//	// 0 = Rectangle.
	//	// 1 = Line.
	//	// 2 = Circle.
	//	unsigned char Type;
	//};

	//std::vector<Batch> mBatch;

	Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
	Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
	Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
	Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
	Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
	Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;
};
